package com.yyfax.ph.personal.feedback;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.R;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.base.YJPH;

public class FeedBackActivity extends BaseYYFaxActivity implements FeedBackPresenter.View {
	private final static int LOADER_ID = 103;

	/**
	 * 输入框
	 */
	@BindView(R.id.input_tv)
	EditText mInputTv;
	/**
	 * 提示输入多少字
	 */
	@BindView(R.id.input_indicator_tv)
	TextView mIndicatorTv;
	/**
	 * 发送按钮
	 */
	@BindView(R.id.feed_back_send_btn)
	TextView mFeedBackSendBtn;

	/**
	 * 输入表情前的光标位置
	 */
	private int cursorPos;
	/**
	 * 输入表情前EditText中的文本
	 */
	private String inputAfterText;
	/**
	 * 是否重置了EditText的内容
	 */
	private boolean resetText;

	private FeedBackPresenter mPresenter;

	public static void start(Activity aty) {
		Intent intent = new Intent(aty, FeedBackActivity.class);
		aty.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getSupportLoaderManager().destroyLoader(LOADER_ID);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(this, new FeedBackPresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		mPresenter = (FeedBackPresenter) data;
		mPresenter.attach(this);
	}

	@Override
	public int getContentViewRes() {
		return R.layout.feed_back_activity_layout;
	}

	@Override
	public void initContentView() {
		super.initContentView();
		setBtnRightVisible(false);
		setTitleVisible(true);
		setTitle(R.string.question_feed_back);
		setTitleLeftVisible(false);
		setIndicatorText(R.string.input_indicator, "0");

		mInputTv.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int count) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
				String content = mInputTv.getText().toString();
				//显示已经输入多好个字了
				if (!TextUtils.isEmpty(content)) {
					setIndicatorText(R.string.input_indicator, String.valueOf(content.length()));
				} else {
					setIndicatorText(R.string.input_indicator, "0");
				}
			}
		});
	}

	private void setIndicatorText(int source, String args) {
		mIndicatorTv.setText(getResources().getString(source, args));
	}

	@Override
	public void onBackBtnPressed() {
		super.onBackBtnPressed();
		finish();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	private void sendFeedBack() {
		String content = mInputTv.getText().toString();
		if (TextUtils.isEmpty(content)) {
			showToast(String.format(getString(R.string.please_input_toast), getString(R.string.feed_back_content)));
		} else {
			mPresenter.feedBack(content);
		}
	}

	@Override
	public void sendFeedBackSuccess(String tips) {
		dismissLoading();
		showToast(getString(R.string.send_succeed));
		finish();
	}

	@Override
	public void sendFeedBackFailure(String tips) {
		dismissLoading();
		showToast(tips);
	}

	/**
	 * 判断是否包含表情
	 */
	public static boolean containsEmoji(String source) {
		int len = source.length();
		for (int i = 0; i < len; i++) {
			char codePoint = source.charAt(i);
			if (!isEmojiCharacter(codePoint)) { // 如果不能匹配,则该字符是Emoji表情
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断是否是Emoji
	 *
	 * @param codePoint 比较的单个字符
	 * @return
	 */
	private static boolean isEmojiCharacter(char codePoint) {
		return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
				|| ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) &&
				(codePoint <= 0xFFFD)) || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
	}

	@OnClick(R.id.feed_back_send_btn)
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
			case R.id.feed_back_send_btn:
				StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_E017);
				sendFeedBack();
				break;
		}
	}
}
